package main

import (
	"fmt"
	"sync"
)

var resource []string

func initResource() {
	fmt.Println("initResource")
	if resource == nil {
		fmt.Println("init")
		resource = make([]string, 10)
	}
}

func main() {
	// go run ch9.5.go -race--竞态检测
	var n = sync.WaitGroup{}
	for i := 0; i < 100; i++ {
		go func() {
			n.Add(1)
			initResource()
			n.Done()
		}()
	}
	n.Wait()

	//var loadOnce = sync.Once{}
	//for i := 0; i < 20; i++ {
	//	go loadOnce.Do(initResource)
	//}
	//fmt.Println(resource == nil)
}
